package com.mf.morefun.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mf.morefun.R;
import com.mf.morefun.bean.ReadTypeBean;
import com.zhcx.zhcxlibrary.utils.DeviceUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ReadTypeAdapter extends BaseAdapter {
    private Context mContext;
    private List<ReadTypeBean> readTypeBeanList;
    private int selectItem = 0;

    public int getSelectItem() {
        return selectItem;
    }

    public void setSelectItem(int selectItem) {
        this.selectItem = selectItem;
    }

    public ReadTypeAdapter(Context mContext, List<ReadTypeBean> readTypeBeanList) {
        this.mContext = mContext;
        this.readTypeBeanList = readTypeBeanList;
    }

    @Override
    public int getCount() {
        return readTypeBeanList.size();
    }

    @Override
    public Object getItem(int position) {
        return readTypeBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        if (convertView == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.layout_readtypeitem, null);
        } else {
            view = convertView;
        }
        if (null != readTypeBeanList && readTypeBeanList.size() > 0) {
            ViewHolder holder = new ViewHolder(view);
            if (getSelectItem() == position) {
                holder.tvType.setTextColor(mContext.getResources().getColor(R.color.white));
                holder.llType.setBackgroundResource(R.mipmap.mineselected);
            } else {
                holder.tvType.setTextColor(0xff333333);
                holder.llType.setBackgroundResource(R.mipmap.noselect);
            }
            holder.tvType.setText(readTypeBeanList.get(position).getClassification().getName());
            ViewGroup.LayoutParams params = holder.llType.getLayoutParams();
            if(null != params){
                params.height = DeviceUtils.dip2px(mContext, 38);
                params.width = ViewGroup.LayoutParams.MATCH_PARENT;
            }else {
                params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DeviceUtils.dip2px(mContext, 38));
            }
            holder.llType.setLayoutParams(params);
        }
        return view;
    }

    static class ViewHolder {
        @BindView(R.id.tv_type)
        TextView tvType;
        @BindView(R.id.ll_type)
        LinearLayout llType;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
